import {Pipe, PipeTransform} from '@angular/core';

@Pipe({name: 'dateAgoPipe'})
export class DateAgoPipe implements PipeTransform {
  transform(value: any): string {
    let date: Date;
    if (value instanceof Array) {//如果是数组,则按照java中对应的LocalDateTime方式处理
      if (value.length == 6) {
        date = new Date(value[0] + "-" + value[1] + "-" + value[2] + " " + value[3] + ":" + value[4] + ":" + value[5])
      } else if (value.length == 3) {
        date = new Date(value[0] + "-" + value[1] + "-" + value[2])
      } else {
        date = null;
      }
    } else if (!isNaN(value)) {
      date = new Date(value)
    } else if (typeof value == "string") {
      date = new Date(value)
    }

    if (!date)return null;
    const timeDifference: number = (new Date().getTime() - date.getTime()) / 1000;
    if (timeDifference <= 5 * 60)
      return "刚刚";
    if (timeDifference <= 10 * 60)
      return "5分钟前";
    if (timeDifference < 30 * 60)
      return "10分钟前";
    if (timeDifference < 60 * 60)
      return "30分钟前";
    if (timeDifference < 24 * 60 * 60)
      return "30分钟前";
    if (timeDifference < 24 * 60 * 60)
      return "今天";

    if (timeDifference < 2 * 24 * 60 * 60)
      return "1天前";
    if (timeDifference < 3 * 24 * 60 * 60)
      return "2天前";
    if (timeDifference < 10 * 24 * 60 * 60)
      return "3天前";
    if (timeDifference < 30 * 24 * 60 * 60)
      return "10天前";
    if (timeDifference < 6 * 30 * 24 * 60 * 60)
      return "1个月前";
    if (timeDifference < 12 * 30 * 24 * 60 * 60)
      return "半年前";
    if (timeDifference >= 12 * 30 * 24 * 60 * 60)
      return "1年前";
  }
}
